// plugins/console_plugin/console_plugin.c
#include "plugin.h"      // Defines PluginHandle, PluginInfo, function types
#include "mpack/mpack.h" // Include MPack for node API access
#include <stdio.h>
#include <stdlib.h>
#include <string.h>      // For strdup (if needed later)

// Plugin internal data structure (optional, could be more complex)
typedef struct {
    int counter;
    // Add other plugin-specific state here if needed
} ConsolePluginData;


// --- Exported Functions ---

// Plugin information (remains the same)
PluginInfo get_plugin_info_export() {
    static const PluginInfo info = { // Use static const for efficiency
        "console_plugin",          // Plugin name
        "A simple console plugin", // Plugin description
        100                        // Plugin API version
    };
    return info;
}

// Plugin load function
bool plugin_load_export(PluginHandle* handle) {
    printf("Console plugin loading...\n");
    // Allocate memory for plugin-specific data
    ConsolePluginData* data = (ConsolePluginData*)malloc(sizeof(ConsolePluginData));
    if (data == NULL) {
        perror("Failed to allocate memory for console plugin data");
        *handle = NULL;
        return false;
    }
    data->counter = 0; // Initialize data
    *handle = (PluginHandle)data; // Use the allocated struct address as the handle
    printf("Console plugin loaded successfully. Handle: %p\n", *handle);
    return true;
}

// Plugin unload function
bool plugin_unload_export(PluginHandle handle) {
    printf("Console plugin unloading. Handle: %p\n", handle);
    if (handle != NULL) {
        free(handle); // Free the allocated ConsolePluginData struct
    }
    printf("Console plugin unloaded successfully.\n");
    return true;
}

// Plugin start function (MODIFIED)
bool plugin_start_export(PluginHandle handle, mpack_node_t config_node) {
    printf("Console plugin starting. Handle: %p\n", handle);
    ConsolePluginData* data = (ConsolePluginData*)handle;

    if (data == NULL) {
        fprintf(stderr, "Error: Invalid handle passed to console_plugin_start.\n");
        return false;
    }

    // --- Configuration Parsing ---
    const char* message = "Default message: Plugin started."; // Default value
    bool is_verbose = false; // Default value

    // Check if the provided config node is actually a map
    if (mpack_node_type(config_node) != mpack_type_map) {
        fprintf(stderr, "Warning: Console plugin configuration is not a map. Using default values.\n");
    } else {
        printf("  Parsing configuration node...\n");

        // 1. Try to get the "message" string
        mpack_node_t message_node = mpack_node_map_cstr(config_node, "message");
        if (!mpack_node_is_missing(message_node)) { // Check if key exists
            if (mpack_node_type(message_node) == mpack_type_str) {
                message = mpack_node_str(message_node);
                // Note: 'message' points into the PluginManager's buffer.
                // If needed long-term, use strdup:
                // char* stored_message = strdup(mpack_node_str(message_node));
                printf("  Config found - message: \"%s\"\n", message);
            } else {
                fprintf(stderr, "Warning: Config key 'message' is not a string. Using default.\n");
            }
        } else {
             printf("  Config key 'message' not found. Using default.\n");
        }

        // 2. Try to get the "verbose" boolean
        mpack_node_t verbose_node = mpack_node_map_cstr(config_node, "verbose");
         if (!mpack_node_is_missing(verbose_node)) { // Check if key exists
            if (mpack_node_type(verbose_node) == mpack_type_bool) {
                is_verbose = mpack_node_bool(verbose_node);
                 printf("  Config found - verbose: %s\n", is_verbose ? "true" : "false");
            } else {
                fprintf(stderr, "Warning: Config key 'verbose' is not a boolean. Using default.\n");
            }
        } else {
             printf("  Config key 'verbose' not found. Using default.\n");
        }
    }
    // --- End Configuration Parsing ---


    // Use the configuration
    printf("Console Plugin Says: %s\n", message);

    if (is_verbose) {
        printf("Console plugin internal counter (verbose mode): %d\n", data->counter);
    }

    // Plugin start-up logic (e.g., modify internal state)
    data->counter++;

    printf("Console plugin started successfully.\n");
    return true;
}

// Plugin stop function
bool plugin_stop_export(PluginHandle handle) {
    printf("Console plugin stopping. Handle: %p\n", handle);
     ConsolePluginData* data = (ConsolePluginData*)handle;
     if (data != NULL) {
         printf("  Final counter value: %d\n", data->counter);
     }
    printf("Console plugin stopped successfully.\n");
    return true;
}
